package manage.actions.action;

import manage.actions.ManageAction;
import manage.commons.annontations.RequiredLogin;
import manage.commons.annontations.SuperManager;
import manage.commons.utils.InputStreamUtil;
import manage.commons.utils.Pager;
import manage.modules.action.model.ActionUri;
import manage.modules.action.service.ActionUriService;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

@RequiredLogin
@SuperManager
public class ActionUriAction extends ManageAction {

	private static final long serialVersionUID = 4249886187419780524L;

	private ActionUriService actionUriService;
	private Pager<ActionUri> pager = new Pager<ActionUri>(100);
	private ActionUri actionUri;

	@Override
	@Action("action-input")
	public String execute() {
		return SUCCESS;
	}

	@Override
	@Action("action-uri-input")
	public String input() {
		return SUCCESS;
	}

	@Override
	@Action(value = "action-uri-addorupdate", results = { @Result(name = SUCCESS, location = "/action/action-uri-form.jsp") })
	public String forwardAddOrUpdatePage() {
		if (isAddOperater()) {
			action = "/action/action-uri-add.do";
			getRequest().setAttribute("btnKeyWord", "添加访问路径");
		} else if (isUpdateOperater()) {
			action = "/action/action-uri-edit.do";
			getRequest().setAttribute("btnKeyWord", "更新访问路径");
			actionUri = actionUriService.select(actionUri.getActionId());
		}
		return SUCCESS;
	}

	@Action(value = "action-uri-query", results = { @Result(name = SUCCESS, location = "/action/action-uri-list.jsp") })
	public String query() throws Exception {
		if (actionUri != null) {
			actionUriService.query(actionUri, pager);
			pager.pagerUtils(pager, request);
		} else {
			pager.setList(actionUriService.listAll());
			pager.setRowCount(actionUriService.getCountOfAll());
			pager.pagerUtils(pager, request);
		}
		return SUCCESS;
	}

	@Action(value = "action-uri-select", results = { @Result(name = SUCCESS, type = "json", params = {
			"root", "action" }) })
	public String select() {
		if (null != actionUri && null != actionUri.getActionId()) {
			boolean exists = actionUriService.exists(actionUri.getActionId());
			if (exists) {
				actionUri = actionUriService.select(actionUri.getActionId());
			}
		}
		return SUCCESS;
	}

	@Action(value = "action-uri-add", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName", "inputStream" }) })
	public String add() throws Exception {
		if (null != actionUri && actionUri.getActionName() != null
				&& actionUri.getActionUri() != null && actionUri.getNote() != null) {
			boolean exists = actionUriService.exists(actionUri.getActionUri());
			if (!exists) {
				int count = actionUriService.insert(actionUri);
				if (count == 1) {
					inputStream = InputStreamUtil.getSuccessInputStream();
				}
			} else {
				inputStream = InputStreamUtil.getExistsInputStream();
			}
		} else {
			inputStream = InputStreamUtil.getFailedInputStream();
		}
		return SUCCESS;
	}

	@Action(value = "action-uri-delete", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName", "inputStream" }) })
	public String delete() throws Exception {
		if (null != actionUri && null != actionUri.getActionId()) {
			boolean exists = actionUriService.exists(actionUri.getActionId());
			if (exists) {
				int delNum = actionUriService.delete(actionUri.getActionId());
				if (delNum == 1) {
					inputStream = InputStreamUtil.getSuccessInputStream();
				}
			} else {
				inputStream = InputStreamUtil.getNotExistsInputStream();
			}
		} else {
			inputStream = InputStreamUtil.getFailedInputStream();
		}
		return SUCCESS;
	}

	@Action(value = "action-uri-edit", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName", "inputStream" }) })
	public String edit() throws Exception {
		if (null != actionUri && null != actionUri.getActionId()
				&& actionUri.getActionName() != null && actionUri.getActionUri() != null
				&& actionUri.getNote() != null) {
			boolean exists = actionUriService.exists(actionUri.getActionId());
			if (exists) {
				boolean existsUri = actionUriService.exists(actionUri.getActionUri());
				ActionUri atUri = actionUriService.select(actionUri.getActionId());
				if (!existsUri
						|| StringUtils.equals(atUri.getActionUri(), actionUri.getActionUri())) {
					int count = actionUriService.update(actionUri);
					if (count >= 1) {
						inputStream = InputStreamUtil.getSuccessInputStream();
					}
				} else {
					inputStream = InputStreamUtil.toInputStream("existUri");
				}
			} else {
				inputStream = InputStreamUtil.getNotExistsInputStream();
			}
		} else {
			inputStream = InputStreamUtil.getFailedInputStream();
		}
		return SUCCESS;
	}

	public Pager<ActionUri> getPager() {
		return pager;
	}

	public void setPager(Pager<ActionUri> pager) {
		this.pager = pager;
	}

	public void setActionService(ActionUriService actionUriService) {
		this.actionUriService = actionUriService;
	}

	public ActionUri getActionUri() {
		return actionUri;
	}

	public void setActionUri(ActionUri actionUri) {
		this.actionUri = actionUri;
	}

	public void setActionUriService(ActionUriService actionUriService) {
		this.actionUriService = actionUriService;
	}
}
